#TouhouDanmakufu[Stage]
#Title[zQIs \Phantasmagoria Trues\@XyJ[hvNeBX]
#Text[ver 1.00]
#BGM[]
#Player[FREE]
#BackGround[]
#ScriptVersion[2]
script_stage_main
{
	#include_function".\GameData\SpellPracticeData\LocalData.txt";
	#include_function".\GameData\SystemData\SystemBase.txt";
	@Initialize{ProjectInitialize_SP;}
	@MainLoop{GameMainLoopSystem;yield;}
	@BackGround{GameBackGroundSystem;}
	@DrawTopObject{GameTopObjectSystem_SP;}
	@Finalize{ProjectFinalize_SP;}

function ProjectInitialize_SP
{
	CreateCommonDataArea("Temp");
	SET_SPELLCARD_PRACTICE;
	SetRateScoreSystemEnable(false);
	SetDefaultStatusVisible(false);
	SetShotAutoDeleteClip(80,80,80,80);
	
	if(!debug_mode){ExpertEx(false,0,3);}
	
	AddScore(1);

	MemoryLoad_SP;
	
	if(memory_flg[10]&&memory_flg[11]&&memory_flg[12])
	{
		if(tuning_wallpaper==1){
			tuning_wallpaper=2;
		}
		if(!memory_flg[9]){
		//	memory_flg[9]=true;
		}
	}
	if(memory_flg[13]){
		if(tuning_wallpaper==2){
			tuning_wallpaper=3;
		}
	}

	if(tuning_wallpaper==1)
	{
		tex[TEX_FRAMEBACK]=current_tex~"Tex_FrameBackground2.png";
		tex[TEX_FRAMEBACK2]=current_tex~"Tex_FrameBackground2_r.png";
		bgm=[current_bgm~"title3.wav",
		 	 current_bgm~"title4.wav"];
		
	}
	if(tuning_wallpaper==2)
	{
		tex[TEX_FRAMEBACK]=current_tex~"Tex_FrameBackground3.png";
		tex[TEX_FRAMEBACK2]=current_tex~"Tex_FrameBackground3_r.png";
		bgm=[current_bgm~"title5.wav",
		 	 current_bgm~"title6.wav"];
	}
	if(tuning_wallpaper==3){
		tex[TEX_FRAMEBACK]=current_tex~"Tex_FrameBackground4.png";
		tex[TEX_FRAMEBACK2]=current_tex~"Tex_FrameBackground4_r.png";
		bgm=[current_bgm~"title7.wav",
		 	 current_bgm~"title7.wav"];
	}

	ascent(a in 0..length(tex)){LoadGraphic(tex[a]);}
	ascent(a in 0..length(se)){LoadSE(se[a]);}
	
	SetCommonData("Score",score);
	SetCommonData("SinglePlayFlg",false);
	
	if(IsReplay){replayflg=true;}
	GameMainThread_SP;
}


task GameMainThread_SP
{
	yield;
	CreateEnemyFromFile(GetCurrentScriptDirectory~".\GameData\LibData\load_usershot.txt",0,0,0,0,0);

	if(!IsReplay)
	{
	SetCommonData("SpellResult_TotalCount",GetCommonDataDefaultEx("Temp","Memory_SpellResult_TotalCount_SP",DefaultArray(0)));
	SetCommonData("SpellResult_GetCount",GetCommonDataDefaultEx("Temp","Memory_SpellResult_GetCount_SP",DefaultArray(0)));

	SetCommonData("Memory_SpellResult_TotalCount_SP",GetCommonDataDefault("SpellResult_TotalCount",DefaultArray(0)));
	SetCommonData("Memory_SpellResult_GetCount_SP",GetCommonDataDefault("SpellResult_GetCount",DefaultArray(0)));
	}
	else
	{
	//	SetCommonData("Memory_SelectedSpell_TotalCount",GetCommonDataDefaultEx("Temp","Memory_SpellResult_TotalCount_SP_Replay",1));
	//	SetCommonData("Memory_SelectedSpell_GetCount",GetCommonDataDefaultEx("Temp","Memory_SpellResult_GetCount_SP_Replay",1));
	SetCommonData("SpellResult_TotalCount",GetCommonDataDefault("Memory_SpellResult_TotalCount_SP",DefaultArray(0)));
	SetCommonData("SpellResult_GetCount",GetCommonDataDefault("Memory_SpellResult_GetCount_SP",DefaultArray(0)));
	}

	FrameRender;
	let SelectedSpell=GameMenu_SP;

	gamemode=SPELLCARD_PRACTICE_GameMode(SelectedSpell);

	SetCommonData("PlayMode",gamemode);

	ItemBorderLine;
	DrawMultiplyCount;
	DrawBaseCount;

	if(gamemode==0||gamemode==1){ExStageFlgUnlock;}
	
	frame_rflg=SPELLCARD_PRACTICE_FRAME_REVERSE(SelectedSpell);

	if(!Retry_Flg_SP)
	{
	wait(45);
	}

	SetMusic(SPELLCARD_PRACTICE_Music(SelectedSpell),BGM_LOAD);
	SetMusic(SPELLCARD_PRACTICE_Music(SelectedSpell),BGM_PLAY);

	ForbidBomb(true);

	let csd=GetCurrentScriptDirectory;
	let BossDir=GetCurrentScriptDirectory~"GameData/EnemyData/Boss/";
	ExpertEx(false,99,0);
	AddBomb(-GetPlayerBomb);
	ExtendPlayer(-(GetPlayerLife-1));
	SetCommonData("SpellPractice_Mode",true);

	loop
	{
		if(Retry_Mode_SP)
		{
		Retry_Flg_SP=false;
		}
		let SpellTotalResult_SP=GetCommonDataDefault("SpellResult_TotalCount",DefaultArray(0));

		SpellTotalResult_SP[SelectedSpell-1]=SpellTotalResult_SP[SelectedSpell-1]+1;

		SetCommonData("SpellResult_TotalCount",SpellTotalResult_SP);
		SetCommonData("Memory_SpellResult_TotalCount_SP",SpellTotalResult_SP);

		CreateEnemyFromFile(GetCurrentScriptDirectory~"GameData/SpellPracticeData/Invincibility.txt",cenX,minY-50,0,0,0);
		CreateEnemyBossFromFile(SPELLCARD_PRACTICE_ARRAY[SelectedSpell],cenX,minY-50,0,0,0);

		while(IsBossExisting){yield;}
		if(GetCommonDataDefault("SpellGetCount",0)>0){break;}
		if(GetCommonDataDefault("SpellPractice_Mode_EndFlg",false)){break;}
		while(GetPlayerLife!=3){yield;}
		if(Retry_Mode_SP)
		{
		Retry_Flg_SP=true;
		Retry;
		}
		yield;
		AddBomb(-GetPlayerBomb);
		ExtendPlayer(-(GetPlayerLife-1));

	}
	let SpellGetResult_SP=GetCommonDataDefault("SpellResult_GetCount",DefaultArray(0));
	SpellGetResult_SP[SelectedSpell-1]=SpellGetResult_SP[SelectedSpell-1];
	SetCommonData("SpellResult_GetCount",SpellGetResult_SP);
	SetCommonData("Memory_SpellResult_GetCount_SP",SpellGetResult_SP);

	SetMusic(SPELLCARD_PRACTICE_Music(SelectedSpell),BGM_FADEOUT_15);

	wait(60);

	Menu_WhiteOut;

	task Menu_WhiteOut
	{
		loop(8)
		{
			WhiteOut;
			wait(2);
		}
		task WhiteOut
		{
			let obj=SetEffectObject(tex[TEX_WHITE],7);
			SetEffectObjectRect(obj,0,0,320,240);
			ObjEffect_SetRenderState(obj,ADD);
			Obj_SetPosition(obj,cenX,cenY);
			
			let count=0;
			let scale=1;
			let alpha=16;
			while(alpha>2)
			{
				ObjEffect_SetScale(obj,scale,scale);
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,alpha,255,255,255);
				}
				if(count>40){alpha-=10;}
				else if(alpha<250){alpha+=2;}
				scale+=0.2;
				count++;
				yield;
			}
			if(GetKeyState(VK_SKIP)==KEY_HOLD)
			{
				Retry;
			}
			else
			{
				SetClearFrameEffectEnable(false);
				ClearStage;
			}
			Obj_Delete(obj);
		}
	}
}


function GameMenu_SP
{
	SetMusic(bgm[play_count%2],BGM_LOAD);
	SetMusic(bgm[play_count%2],BGM_PLAY);
	let menustate=1;
	let menu_from=MENU_TOP;
	let menu_number=0;
	let effect_l=2;
	
	let from_wait=2;
	let frame_wait=1;

	let t_flg=memory_flg[9];
	
	let location_flg=false;
	let location_setting_red=memory_others[4];
	let location_setting_base=memory_others[5];
	if(location_setting_base==0){location_setting_base=10000;}
	
	if(!Retry_Flg_SP)
	{
	Menu_BackGround(32,16,416,464);
	}

	let SpellSelect=GetCommonDataDefaultEx("Temp","Memory_SelectedSpell",1);
	if(!IsReplay)
	{
		SpellSelect=GetCommonDataDefaultEx("Temp","Memory_SelectedSpell",1);
	}
	else
	{
		SpellSelect=GetCommonData("Memory_SelectedSpell");
	}
	let SpellListPage=1;
	let SpellNum=[12,15,18,15,10,21,19,12,16,20,17];
	let SpellTopNum=[1,13,28,46,61,71,92,111,123,139,159];
	let SpellUnderNum=[12,27,45,60,70,91,110,122,138,158,175];
	ascent(i in 0..11)
	{
		if(SpellSelect>=SpellTopNum[i] && SpellSelect<=SpellUnderNum[i])
		{
			SpellListPage=i+1;
		}
	}

	if(!Retry_Flg_SP)
	{
	Menu_SP;
	Menu_Draw_SP;
	MenuState_SP;
	}

	SetCommonData("Temp_Selected_Spell",SpellSelect);
	SetMusic(bgm[play_count%2],BGM_FADEOUT_10);

	menu_flg=false;
	
	return(SpellSelect);

	function MenuState_SP
	{
		while(menu_flg)
		{
			ForbidShot(true);
			ForbidBomb(true);
			SetPlayerX(cenX);
			SetPlayerY(380);
			yield;
		}
		ForbidShot(false);
		
		alternative(gamemode)
		case(0)
		{
			extend_1up=1400;
			extend_bomb=1000;
		}
		case(1)
		{
			extend_1up=1000;
			extend_bomb=1400;
		}
		case(2)
		{
			extend_1up=1200;
			extend_bomb=1400;
		}
		case(3)
		{
			extend_1up=1700*100;
			extend_bomb=1700*100;
		}
		
		if(location_flg)
		{
			alternative(location_setting_red)
			case(0){red_num=0;}
			case(1){red_num=5001;}
			case(2){red_num=8001;}
			case(3){red_num=10000;}
			
			itempoint=location_setting_base;
		}

	}

	task Menu_SP
	{
		loop
		{
			if(GetKeyState(VK_UP)==KEY_PUSH)
			{
				let TopFlag=false;
				ascent(i in 0..11)
				{
					if(SpellSelect==SpellTopNum[i])
					{
						TopFlag=true;
					}
				}
				if(TopFlag)
				{
					SpellSelect+=SpellNum[SpellListPage-1];
				}
				SpellSelect-=1;
				PlaySE(se[SE_MENU_SELECT]);
				wait(frame_wait);
			}
			else if(GetKeyState(VK_DOWN)==KEY_PUSH)
			{
				let UnderFlag=false;
				ascent(i in 0..11)
				{
					if(SpellSelect==SpellUnderNum[i])
					{
						UnderFlag=true;
					}
				}
				if(UnderFlag)
				{
					SpellSelect-=SpellNum[SpellListPage-1];
				}
				SpellSelect+=1;
				PlaySE(se[SE_MENU_SELECT]);
				wait(frame_wait);
			}
			else if(GetKeyState(VK_RIGHT)==KEY_PUSH)
			{
				SpellListPage+=1;
				if(SpellListPage>=12)
				{
					SpellListPage=1;
					SpellSelect-=158;
					if(SpellUnderNum[SpellListPage-1]<SpellSelect)
					{
						SpellSelect=SpellTopNum[SpellListPage-1];
					}
				}
				else
				{
					SpellSelect+=SpellNum[SpellListPage-2];
					if(SpellUnderNum[SpellListPage-1]<SpellSelect)
					{
						SpellSelect=SpellTopNum[SpellListPage-1];
					}
				}
				PlaySE(se[SE_MENU_SELECT]);
				wait(frame_wait);
			}
			else if(GetKeyState(VK_LEFT)==KEY_PUSH)
			{
				SpellListPage-=1;
				if(SpellListPage<=0)
				{
					SpellListPage=11;
					SpellSelect+=158;
					if(SpellUnderNum[SpellListPage-1]<SpellSelect)
					{
						SpellSelect=SpellTopNum[SpellListPage-1];
					}
				}
				else
				{
					SpellSelect-=SpellNum[SpellListPage-1];
					if(SpellUnderNum[SpellListPage-1]<SpellSelect)
					{
						SpellSelect=SpellTopNum[SpellListPage-1];
					}
				}
				PlaySE(se[SE_MENU_SELECT]);
				wait(frame_wait);
			}

			if(GetKeyState(VK_SHOT)==KEY_PUSH)
			{
				if(memory_spell_totalcount[SpellSelect-1]!=0)
				{
				PlaySE(se[SE_MENU_DECIDE]);
				menu_flg=false;
				Menu_WhiteOut;
				wait(20);
				if(Retry_Mode_SP)
				{
					wait(20);
					Retry_Flg_SP=true;
					Retry;
				}
				return;
				}
			}

			if(GetKeyState(VK_BOMB)==KEY_PUSH)
			{
				PlaySE(se[SE_MENU_CANCEL]);
				SpellSelect=1;
				SpellListPage=1;
			}
			SetCommonData("Temp_Selected_Spell",SpellSelect);
			yield;
		}
	}

	task Menu_Draw_SP
	{
		loop
		{

			let DX=310;
			let DY=40;
			ascent(a in SpellTopNum[SpellListPage-1]..SpellUnderNum[SpellListPage-1]+1)
			{
				let num=a;
				let lp=floor(log10(num))+1;
				let leng=16;
				let i=lp;
				loop(lp)
				{
					let posx=16+(lp-i)*(leng/1.4);
					let rc=(num%(10^i)-num%(10^(i-1)))/(10^(i-1));
					Number(rc,posx,DY,0,a);
					i--;
				}
				SpellName(a-1,DX,DY,0);
				SetResult(a-1,DX+40,DY,0);
				DY+=20;
			}
			let OldPage=SpellListPage;
			loop
			{
				if(OldPage!=SpellListPage){break;}
				yield;
			}
			if(!menu_flg){wait(20);return;}

		task SetResult(let num,let sx,let sy,let from,)
		{
			let data_total=GetCommonData("SpellResult_TotalCount");
			let data_get=GetCommonData("SpellResult_GetCount");
			let getnum=data_get[num];
			let totalnum=data_total[num]+1;
			let posy=sy;
			if(totalnum>0)
			{
				let lp=floor(log10(getnum))+1;
				let leng=16;
				let i=lp;
				loop(lp)
				{
					let posx=sx+(lp-i)*(leng/1.4);
					let rc=(getnum%(10^i)-getnum%(10^(i-1)))/(10^(i-1));
					Number(rc,posx,posy,from,num+1);
					i--;
				}
				if(getnum==0)
				{
					Number(0,sx,sy,from,num+1);
				}
				sx+=48;
				Number(10,sx,sy,from,num+1);
				
				sx+=32;
				let lp=floor(log10(totalnum))+1;
				let i=lp;
				loop(lp)
				{
					let posx=sx+(lp-i)*(leng/1.4);
					let rc=(totalnum%(10^i)-totalnum%(10^(i-1)))/(10^(i-1));
					Number(rc,posx,posy,from,num+1);
					i--;
				}
				if(totalnum==0)
				{
					Number(0,sx,sy,from,num+1);
				}
				
				
				sx+=64;
				let pct=0;
				if(getnum>0)
				{
					pct=(getnum/totalnum)*100;
					if(pct>50){pct=floor(pct);}
					else{pct=ceil(pct);}
				}
				
			}
			
		}

		task SpellName(let num,let x,let y,let from)
		{
			let flg=0;
			let rc=0;
			if(memory_spell_totalcount[num]==0){flg=1;}
			while(num>=64){rc++;num-=64;}
			let obj=SetEffectObject(tex[TEX_SPELLNAME],8);
			ObjEffect_SetRenderState(obj,ALPHA);
			if(flg==1){SetEffectObjectRect(obj,1536,1984,2048,2016);}
			else{SetEffectObjectRect(obj,0+512*rc,0+32*num,512+512*rc,32+32*num);}
			Obj_SetPosition(obj,x,y);

			let OldPage=SpellListPage;
			loop
			{
				if(num==(SpellSelect-1)%64)
				{
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,255,255,255,255);
				}
				}
				else
				{
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,255/1.5,255,255,255);
				}
				}

				if(OldPage!=SpellListPage){break;}
				if(!menu_flg){break;}
				yield;
			}
			Obj_Delete(obj);
		}

		task Number(let num,let x,let y,let from,SelectedNumber)
		{
			let obj=SetEffectObject(tex[TEX_SPELLNAME],8);
			ObjEffect_SetRenderState(obj,ALPHA);
			SetEffectObjectRect(obj,1728+16*num,2016,1744+16*num,2032);
			Obj_SetPosition(obj,x,y);


			let OldPage=SpellListPage;
			loop
			{
				if(SelectedNumber==SpellSelect)
				{
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,255,255,255,255);
				}
				}
				else
				{
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,255/1.5,255,255,255);
				}
				}
				if(OldPage!=SpellListPage){break;}
				if(!menu_flg){break;}
				yield;
			}
			Obj_Delete(obj);
		}

		yield;
		}
	}

	function Menu_Frame(let x,let y,let xscale,let yscale,let state)
	{
		let obj=SetEffectObject(tex[TEX_TITLEMENU],8);
		SetEffectObjectRect(obj,384,384,512,512);
		ObjEffect_SetRenderState(obj,ALPHA);
		Run;
		return obj;
		task Run
		{
			let alpha=220;
			let limit=frame_wait;
			let count=0;
			let xsc=0;
			let ysc=0;
			while(!Obj_BeDeleted(obj))
			{
				let sx=x+64*xsc;
				let sy=y+64*ysc;
				Obj_SetPosition(obj,sx,sy);
				ObjEffect_SetScale(obj,xsc,ysc);
				
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,alpha,255,255,255);
				}
				if(menu_from<state)
				{
					xsc-=xscale/limit;
					ysc-=yscale/limit;
					if(count>=limit){Obj_Delete(obj);}
					count++;
				}
				else
				{
					if(xsc<xscale){xsc+=xscale/limit;}
					if(ysc<yscale){ysc+=yscale/limit;}
				}
				yield;
			}
		}
	}

	task Menu_WhiteOut
	{
		loop(8)
		{
			WhiteOut;
			wait(2);
		}
		task WhiteOut
		{
			let obj=SetEffectObject(tex[TEX_WHITE],7);
			SetEffectObjectRect(obj,0,0,320,240);
			ObjEffect_SetRenderState(obj,ADD);
			Obj_SetPosition(obj,cenX,cenY);
			
			let count=0;
			let scale=1;
			let alpha=16;
			while(alpha>2)
			{
				ObjEffect_SetScale(obj,scale,scale);
				ascent(a in 0..4)
				{
					ObjEffect_SetVertexColor(obj,a,alpha,255,255,255);
				}
				if(count>40){alpha-=10;}
				else if(alpha<250){alpha+=2;}
				scale+=0.2;
				count++;
				yield;
			}
			Obj_Delete(obj);
		}
	}

	task Menu_BackGround(let left,let up,let right,let down)
	{
		let obj=SetEffectObject(tex[TEX_FRAMEBACK],7);
		ObjEffect_SetRenderState(obj,ALPHA);
		
		let sx=left+(right-left)/2;
		let sy=up+(down-up)/2;
		let color=255;
		Obj_SetPosition(obj,sx,sy);
		
		let frame_y=0;
		while(!Obj_BeDeleted(obj))
		{
			
			SetEffectObjectRect(obj,left,up+frame_y,right,down+frame_y);
			ascent(a in 0..4)
			{
				ObjEffect_SetVertexColor(obj,a,255,color,color,color);
			}
			if(tuning_wallpaper==0){frame_y+=-1;}
			if(!menu_flg){wait(20);Obj_Delete(obj);}
			yield;
		}
	}
}

function MemorySave_SP
{
	
	local
	{
		let defaultdata=[];
		ascent(a in 0..200)
		{
			let count=[0];
			defaultdata=defaultdata~count;
		}
		memory_spell_totalcount=GetCommonDataDefault("SpellResult_TotalCount",defaultdata);
		memory_spell_getcount=GetCommonDataDefault("SpellResult_GetCount",defaultdata);
	}
	local
	{
		memory_others[0]=timer_hours;
		memory_others[1]=timer_minutes;
		memory_others[2]=timer_seconds;
		
		memory_others[3]=play_count;
	}
	if(!replayflg)
	{
		let SPELLCARD_PRACTICE_SELECTED_SPELL=GetCommonDataDefault("Temp_Selected_Spell",1);
		let SPELLCARD_PRACTICE_TOTAL_COUNT=GetCommonDataDefault("Memory_SpellResult_TotalCount_SP",DefaultArray(0));
		let SPELLCARD_PRACTICE_GET_COUNT=GetCommonDataDefault("Memory_SpellResult_GetCount_SP",DefaultArray(0));

		let tuning=GetCommonData("Memory_ConfigData");
		let trialmode=tuning[4];

		ClearCommonData;

		SetCommonData("Memory_HighScore",memory_highscore);
		SetCommonData("Memory_HighScore_Stage",memory_highscore_stage);
		SetCommonData("Memory_SpellResult_TotalCount",memory_spell_totalcount);
		SetCommonData("Memory_SpellResult_GetCount",memory_spell_getcount);
		SetCommonData("Memory_FlagData",memory_flg);
		SetCommonData("Memory_ConfigData",memory_tuning);
		SetCommonData("Memory_Others",memory_others);

		SetCommonData("Memory_SelectedSpell",SPELLCARD_PRACTICE_SELECTED_SPELL);
		SetCommonData("Memory_SpellResult_TotalCount_SP",SPELLCARD_PRACTICE_TOTAL_COUNT);
		SetCommonData("Memory_SpellResult_GetCount_SP",SPELLCARD_PRACTICE_GET_COUNT);

		SetCommonData("Memory_Retry_Flg",Retry_Flg_SP);
		SetCommonData("Memory_Retry_Mode_SP",Retry_Mode_SP);

		if(!trialmode)
		{
		SaveCommonData;
		}

		SetCommonData("Memory_HighScore",replay_highscore);
		SetCommonData("Memory_HighScore_Stage",replay_highscore_stage);
		SetCommonData("Memory_SpellResult_TotalCount",replay_spell_totalcount);
		SetCommonData("Memory_SpellResult_GetCount",replay_spell_getcount);
		SetCommonData("Memory_FlagData",replay_flg);
		SetCommonData("Memory_ConfigData",replay_tuning);
		SetCommonData("Memory_Others",replay_others);



		SetCommonData("Memory_SelectedSpell",GetCommonDataDefaultEx("Temp","Memory_SelectedSpell_Replay",1));
		SetCommonData("Memory_SpellResult_TotalCount_SP",GetCommonDataDefaultEx("Temp","Memory_SpellResult_TotalCount_SP_Replay",1));
		SetCommonData("Memory_SpellResult_GetCount_SP",GetCommonDataDefaultEx("Temp","Memory_SpellResult_GetCount_SP_Replay",1));

		SaveCommonDataInReplayFile;
	}
}

function DefaultArray(num)
{
	let Array=[];
	ascent(i in 0..178)
	{
		Array=Array~[num];
	}
	return(Array);
}

function MemoryLoad_SP
{
	if(!IsReplay)
	{
	LoadCommonData;
	Retry_Flg_SP=GetCommonDataDefault("Memory_Retry_Flg",false);
	SetCommonDataEx("Temp","Memory_SelectedSpell",GetCommonDataDefault("Memory_SelectedSpell",1));
	SetCommonDataEx("Temp","Memory_SpellResult_TotalCount_SP",GetCommonDataDefault("Memory_SpellResult_TotalCount_SP",DefaultArray(-1)));
	SetCommonDataEx("Temp","Memory_SpellResult_GetCount_SP",GetCommonDataDefault("Memory_SpellResult_GetCount_SP",DefaultArray(0)));

	SetCommonDataEx("Temp","Memory_SelectedSpell_Replay",GetCommonDataDefault("Memory_SelectedSpell",1));
	SetCommonDataEx("Temp","Memory_SpellResult_TotalCount_SP_Replay",GetCommonDataDefault("Memory_SpellResult_TotalCount_SP",DefaultArray(-1)));
	SetCommonDataEx("Temp","Memory_SpellResult_GetCount_SP_Replay",GetCommonDataDefault("Memory_SpellResult_GetCount_SP",DefaultArray(0)));
	ClearCommonData;
	}
	if(!IsReplay){LoadCommonDataEx("",GetCurrentScriptDirectory~"PhantasmagoriaTrues_save.dat");}
	else
	{
		LoadCommonDataFromReplayFile;
		Retry_Flg_SP=GetCommonDataDefault("Memory_Retry_Flg",false);
		Retry_Mode_SP=GetCommonDataDefault("Memory_Retry_Mode_SP",false);
		Retry_Flg_SP=Retry_Mode_SP;
	}

	

	//HighScore
	local
	{
		let defaultdata=[];
		ascent(a in 0..40)
		{
			let scr=[0];
			defaultdata=defaultdata~scr;
		}
		memory_highscore=GetCommonDataDefault("Memory_HighScore",defaultdata);
		memory_highscore_stage=GetCommonDataDefault("Memory_HighScore_Stage",defaultdata);
		
		replay_highscore=GetCommonDataDefault("Memory_HighScore",defaultdata);
		replay_highscore_stage=GetCommonDataDefault("Memory_HighScore_Stage",defaultdata);
	}
	
	//SpellResult
	local
	{
		let defaultdata=[];
		ascent(a in 0..200)
		{
			let count=[0];
			defaultdata=defaultdata~count;
		}

		SetCommonData("SpellResult_TotalCount",GetCommonDataDefault("Memory_SpellResult_TotalCount",defaultdata));
		SetCommonData("SpellResult_GetCount",GetCommonDataDefault("Memory_SpellResult_GetCount",defaultdata));

		memory_spell_totalcount=GetCommonDataDefault("Memory_SpellResult_TotalCount",defaultdata);
		memory_spell_getcount=GetCommonDataDefault("Memory_SpellResult_GetCount",defaultdata);
		
		replay_spell_totalcount=GetCommonDataDefault("Memory_SpellResult_TotalCount",defaultdata);
		replay_spell_getcount=GetCommonDataDefault("Memory_SpellResult_GetCount",defaultdata);
	}
	
	//FlagData
	local
	{
		let defaultdata=[];
		ascent(a in 0..15)
		{
			let flg=[false];
			defaultdata=defaultdata~flg;
		}
		memory_flg=GetCommonDataDefault("Memory_FlagData",defaultdata);
		replay_flg=GetCommonDataDefault("Memory_FlagData",defaultdata);
	}
	
	//Tuning
	local
	{
		let defaultdata=[3,0,1,1,1,0,0,0,0,0];
		memory_tuning=GetCommonDataDefault("Memory_ConfigData",defaultdata);
		replay_tuning=GetCommonDataDefault("Memory_ConfigData",defaultdata);
		
		tuning_difficulty=memory_tuning[0];
		tuning_lightmode=memory_tuning[1];
		tuning_playmusic=memory_tuning[2];
	//	tuning_continue=memory_tuning[3];
		tuning_supermode=memory_tuning[4];
		tuning_releasetype=memory_tuning[5];
		tuning_wallpaper=memory_tuning[6];
	}
	
	//Others
	local
	{
		let defaultdata=[];
		ascent(a in 0..20)
		{
			let data=[0];
			defaultdata=defaultdata~data;
		}
		memory_others=GetCommonDataDefault("Memory_Others",defaultdata);
		replay_others=GetCommonDataDefault("Memory_Others",defaultdata);
		
		timer_hours=memory_others[0];
		timer_minutes=memory_others[1];
		timer_seconds=memory_others[2];
		
		play_count=memory_others[3];
		
		play_count++;
	}

}

function ProjectFinalize_SP
{
	Slow(0);
	AddScore(-GetScore);
	AddScore(GetCommonDataDefault("Temp_Selected_Spell",1));
	SetHighScore;
	MemorySave_SP;
}

function GameTopObjectSystem_SP
{
	SetTexture(tex[TEX_SYSTEM]);
	DrawSystem_Score;
	DrawSystem_MultiplyGuage;
	DrawSystem_PlayerState_SP;
	DrawSystem_ExtendGuage;
	DrawSystem_RedGuage;
}

function DrawSystem_PlayerState_SP
{
	let rect=[];
	let leng=16;
	let leng_h=64;
	let leng_v=16;
	let scale=0.7;
	
	SetAlpha(255);
	
	SetColor(255,255,255);
	
	//Player
	let pos_x=minX+6+(leng_h/2)*scale;
	let pos_y=minY+50+leng_v/2;
	local
	{
		rect=[64,32];
		SetGraphicScale(scale,scale);
		SetGraphicRect(rect[0],rect[1],rect[0]+leng_h,rect[1]+leng_v);
		DrawGraphic(pos_x,pos_y);
	}
	
	let num=GetPlayerLife-1;
	let pos_x=minX+(leng_h*scale)+leng/2;
	
	loop(num)
	{
		rect=[128,32];
		SetGraphicScale(scale,scale);
		SetGraphicRect(rect[0],rect[1],rect[0]+leng,rect[1]+leng);
		DrawGraphic(pos_x,pos_y);
		pos_x+=leng*0.8;
	}
	
	
	//Bomb
	let pos_x=minX+6+(leng_h/2)*scale;
	let pos_y=minY+66+leng_v/2;
	local
	{
		rect=[64,48];
		SetGraphicScale(scale,scale);
		SetGraphicRect(rect[0],rect[1],rect[0]+leng_h,rect[1]+leng_v);
		DrawGraphic(pos_x,pos_y);
	}
	
	let num=GetPlayerBomb;
	let pos_x=minX+(leng_h*scale)+leng/2;
	
	loop(num)
	{
		rect=[128,48];
		SetGraphicScale(scale,scale);
		SetGraphicRect(rect[0],rect[1],rect[0]+leng,rect[1]+leng);
		DrawGraphic(pos_x,pos_y);
		pos_x+=leng*0.8;
	}
	
	
	
	//GameMode
	if(gamemode!=3)
	{
		if(GetPlayerX>maxX-60){SetAlpha(255+((maxX-60)-GetPlayerX)*4);}
		else{SetAlpha(255);}
		let pos_x=maxX-8;
		let pos_y=cenY+144;
		leng_h=16;
		leng_v=144;
		local
		{
			rect=[464,128];
			alternative(gamemode)
			case(1){
				rect=[480,128];
			}
			case(2){
				rect=[496,128];
			}
			SetGraphicScale(1,1);
			SetGraphicRect(rect[0],rect[1],rect[0]+leng_h,rect[1]+leng_v);
			DrawGraphic(pos_x,pos_y);
		}
		
		
		//TrialMode
		if(tuning_supermode)
		{
			pos_y=cenY+24;
			leng_v=80;
			local
			{
				rect=[432,128];
				SetGraphicScale(1,1);
				SetGraphicRect(rect[0],rect[1],rect[0]+leng_h,rect[1]+leng_v);
				DrawGraphic(pos_x,pos_y);
			}
		}
	}
}

}